package com.dachuang.canjiren.service.impl;

import com.dachuang.canjiren.entity.*;
import com.dachuang.canjiren.entity.VO.LawCommentVO;
import com.dachuang.canjiren.mapper.LawCommentMapper;
import com.dachuang.canjiren.mapper.LawMapper;
import com.dachuang.canjiren.mapper.UserMapper;
import com.dachuang.canjiren.service.LawCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dachuang.common.exception.BizCodeEnume;
import com.dachuang.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-27
 */
@Service
public class LawCommentServiceImpl extends ServiceImpl<LawCommentMapper, LawComment> implements LawCommentService {
    @Autowired
    private LawCommentMapper lawCommentMapper;
    @Autowired
    private LawMapper lawMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public R insertLawComment(Integer lawId, String content, Integer parId, Integer userId) {
        if (Objects.isNull(userId)){
            return R.error(BizCodeEnume.ERROR20006);
        }
        if (Objects.isNull(lawId)) {
            return R.error(BizCodeEnume.VAILD_EXCEPTION);
        }

        if (StringUtils.isEmpty(content)){
            return R.error(BizCodeEnume.ERROR30002);
        }

        //验证userId
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)){
            return R.error(BizCodeEnume.ERROR20004);
        }
        //验证lawId
        Law law = lawMapper.selectLaw(lawId);
        if (Objects.isNull(law)){
            return R.error(BizCodeEnume.ERROR30003);
        }

        //插入到表
        LawComment lawComment = new LawComment();
        lawComment.setLawId(lawId);
        lawComment.setContent(content);
        lawComment.setParId(parId);
        lawComment.setUserId(userId);
        if (lawCommentMapper.insertLawComment(lawComment) > 0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R selectLawCommentList(Integer lawId) {
        if (Objects.isNull(lawId)){
            return R.error(BizCodeEnume.VAILD_EXCEPTION);
        }
        List<LawCommentVO> lawCommentVOS = lawCommentMapper.selectLawCommentList(lawId);
        R r = R.ok();
        r.put("data",lawCommentVOS);
        return r;
    }
}
